<?php

declare(strict_types=1);

namespace app\model;

use think\model\concern\SoftDelete;

/**
 * 
 *
 * @mixin \think\Model
 * @property-read \app\model\AgentSmsLog[] $log
 * @method static \think\db\Query onlyTrashed()
 * @method static \think\db\Query withTrashed()
 */
class SmsModule extends CommonModel
{
    use SoftDelete;

    public function log()
    {
        return $this->hasMany(AgentSmsLog::class, 'module_id');
    }

    public function adminModuleList($where, $paginate, $whereNull = [], $field = '*')
    {
        $query = $this->where($where)->field($field)->order('created_at', 'desc');
        if ($whereNull) {
            foreach ($whereNull as $k => $v) {
                if ($v) {
                    $query = $query->whereNotNull($k);
                } else {
                    $query = $query->whereNull($k);
                }
            }
        }
        $list = $query->paginate()->toArray();
        $statusArr = [0 => 'undo_count', 1 => 'fail_count', 2 => 'succ_count'];
        $statusInitArr = array_combine(array_values($statusArr), [0, 0, 0]);

        if ($list['data']) { // 查询对应发送明细
            $moduleDetail = [];
            $idArr = array_column($list['data'], 'id');
            $detailList = AgentSmsLog::whereIn('module_id', $idArr)->field('count(1) as count,status,module_id')->group('module_id,status')->select();
            if ($detailList) {
                foreach ($detailList as $k => $v) {
                    if (isset($moduleDetail[$v->module_id])) {
                        $moduleDetail[$v->module_id][$statusArr[$v->status]] += $v->count;
                        $moduleDetail[$v->module_id]['all_count'] += $v->count;
                    } else {
                        $moduleDetail[$v->module_id] = $statusInitArr;
                        $moduleDetail[$v->module_id]['all_count'] = $v->count;
                        $moduleDetail[$v->module_id][$statusArr[$v->status]] = $v->count;
                    }
                }
            }

            if ($moduleDetail) {
                foreach ($list['data'] as $k => $v) {
                    $list['data'][$k] = array_merge($v, $moduleDetail[$v['id']]);
                }
            }
        }

        return $list;

        return $this->where($where)->field($field)
        ->withCount(['log' => 'all_count'])
        ->withCount(['log' => function ($query, &$alias) {
            $query->where('status', 1);
            $alias = 'fail_count';
        }])
        ->withCount(['log' => function ($query, &$alias) {
            $query->where('status', 2);
            $alias = 'succ_count';
        }])
        ->withCount(['log' => function ($query, &$alias) {
            $query->where('status', 0);
            $alias = 'undo_count';
        }])
        ->order('created_at', 'desc')
        ->paginate($paginate);
    }

    public static function onAfterDelete($module)
    {
        AgentSmsLog::destroy(AgentSmsLog::where('module_id', $module->id)->column('id'));
    }
}
